package com.yunxin.customui.service.handlerimpl.table;

import com.yunxin.customui.bean.table.TdBean;
import com.yunxin.customui.bean.table.HandlerConditionEnum;
import com.yunxin.customui.bean.table.HandlerParam;
import com.yunxin.customui.myinterface.table.ITdHandler;
import org.springframework.stereotype.Service;

/**
 * 改变颜色
 *
 * @author LGY
 * @date 2021/08/12
 */
@Service
public class CalNumber implements ITdHandler {


    /**
     * 处理程序
     * @param  tdBean 表格单元格对象
     * @param param 参数  Map<id,参数对象>
     */
    @Override
    public void handler(TdBean tdBean, HandlerParam param) {
        try {
            if (param != null && tdBean.getValue()!=null) {
                if (param.getHandlerConditionEnum() == HandlerConditionEnum.加数字) {
                        double v = Double.parseDouble(tdBean.getValue());
                        double v1 = Double.parseDouble(param.getParam());
                        tdBean.setValue(String.valueOf(v+v1));
                }else if (param.getHandlerConditionEnum() == HandlerConditionEnum.加字符) {
                    if (!tdBean.getValue().equals("-999")&&!tdBean.getValue().equals("-999.0")
                            &&!tdBean.getValue().equals("-9999")&&!tdBean.getValue().equals("-9999.0")
                    &&tdBean.getValue().equals("/")&&tdBean.getValue().equals("-")){
                        tdBean.setValue(tdBean.getValue()+param.getParam());
                    }
                }else if (param.getHandlerConditionEnum() == HandlerConditionEnum.减) {
                        double v = Double.parseDouble(tdBean.getValue());
                        double v1 = Double.parseDouble(param.getParam());
                        tdBean.setValue(String.valueOf(v-v1));
                }else if (param.getHandlerConditionEnum() == HandlerConditionEnum.乘) {
                        double v = Double.parseDouble(tdBean.getValue());
                        if (v==0){
                            tdBean.setValue(String.valueOf(v));
                        }else {
                            double v1 = Double.parseDouble(param.getParam());
                            tdBean.setValue(String.valueOf(v*v1));
                        }
                }else if (param.getHandlerConditionEnum() == HandlerConditionEnum.除) {
                        double v = Double.parseDouble(tdBean.getValue());
                        double v1 = Double.parseDouble(param.getParam());
                        tdBean.setValue(String.valueOf(v/v1));
                }
            }
        }catch (NumberFormatException e){
            //跳过文字转换
        }


    }
}
